package com.yifi.system.service;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.SysUtil;
import com.yifi.common.utils.DbUtils;

import cn.hutool.core.util.StrUtil;

public class EamMainService {

	public boolean addEam(String c_pro, String cname, String prefix) {
		String sql = "select * from eam_main where c_pro='" + c_pro + "' and c01='" + prefix + "'";
		Record record = Db.findFirst(sql);
		boolean isedit = true;
		if (record == null) {
			isedit = false;
			record = new Record();
			record.set("id", SysUtil.getGuid());
			record.set("c00", "产权");
			record.set("c01", prefix);
			record.set("c_pro", c_pro);
			record.set("m_id", "00");

		}
		record.set("c02", cname);
		record.set("cpy", SysUtil.getFirstSpell(cname));
		if (DbUtils.save("eam_main", "id", record, isedit)) {
			return true;
		} else {
			return false;
		}

	}

	public List<Record> getTree(List<Record> plist, String c_pro) {
		return getTree("id", "name", plist, c_pro);
	}

	public List<Record> getTree(String cvalue, String ctext, List<Record> plist, String c_pro) {
		String sql = "select c01 as " + cvalue + ",c02 as " + ctext + ",m_id from eam_main where m_id='ROOT' ";
		if (StrUtil.isNotEmpty(c_pro)) {
			sql += " and c_pro='" + c_pro + "'";
		}
		sql += " order by c01";
		List<Record> list = Db.find(sql);
		if (plist != null) {
			for (Record r : plist) {
				String ccode = r.getStr(cvalue);
				// int ilayer=Integer.parseInt(r.getStr("ilayer"))+1;
				String m_id = ccode;
				sql = "select c01 as " + cvalue + ",c02 as " + ctext + ",m_id from eam_main where c01 like '" + ccode
						+ "%' and m_id='" + m_id + "'";
				if (StrUtil.isNotEmpty(c_pro)) {
					sql += " and c_pro='" + c_pro + "'";
				}
				sql += " order by c01";
				List<Record> clist = Db.find(sql);
				if (clist.size() > 0) {
					r.set("open", true);
					r.set("children", getTree(cvalue, ctext, clist, c_pro));
				}
			}

			return plist;
		} else {
			return getTree(cvalue, ctext, list, c_pro);
		}
	}

	public List<Record> getTree(String cvalue, String ctext, List<Record> plist, String bmclass, String c_pro) {
		String sql = "select c01 as " + cvalue + ",c02 as " + ctext + ",m_id from eam_main where m_id='0' ";
		if (StrUtil.isNotEmpty(c_pro)) {
			sql += " and c_pro='" + c_pro + "'";
		}
		sql += " order by c01";
		List<Record> list = Db.find(sql);
		if (plist != null) {
			for (Record r : plist) {
				String ccode = r.getStr(cvalue);
				// int ilayer=Integer.parseInt(r.getStr("ilayer"))+1;
				String m_id = ccode;
				sql = "select c01 as " + cvalue + ",c02 as " + ctext + ",m_id from eam_main where c01 like '" + ccode
						+ "%' and m_id='" + m_id + "'";
				if (StrUtil.isNotEmpty(c_pro)) {
					sql += " and c_pro='" + c_pro + "'";
				}

				sql += " order by c01";
				List<Record> clist = Db.find(sql);
				if (clist.size() > 0) {
					r.set("open", true);
					r.set("children", getTree(cvalue, ctext, clist, c_pro));
				}
			}

			return plist;
		} else {
			return getTree(cvalue, ctext, list, "", c_pro);
		}
	}

	public List<Record> getTreeIdPid(String cvalue, String ctext, List<Record> plist) {
		String sql = "select c01 as " + cvalue + ",c02 as " + ctext
				+ ",m_id,m_id as pid from eam_sblx where m_id='ROOT'";
		List<Record> list = Db.find(sql);
		if (plist != null) {
			for (Record r : plist) {
				String ccode = r.getStr(cvalue);
				int ilayer = Integer.parseInt(r.getStr("ilayer")) + 1;
				sql = "select ccode as " + cvalue + ",cname as " + ctext + ",ilayer,'" + ccode
						+ "' as pid from mis_org where ccode like '" + ccode + "%' and ilayer='" + ilayer + "'";
				List<Record> clist = Db.find(sql);
				/*
				 * if(clist.size()>0) { r.set("open", true); r.set("children",
				 * getTreeIdPid(cvalue,ctext,clist)); }
				 */
			}

			return plist;
		} else {
			return getTreeIdPid(cvalue, ctext, list);
		}
	}

	public List<Record> getFlTree(String cvalue, String ctext, List<Record> plist, String c_pro) {
		String sql = "select c01 as " + cvalue + ",c02 as " + ctext + ",m_id,n03 from eam_sblx where m_id='ROOT'";
		if (StrUtil.isNotEmpty(c_pro)) {
			sql += " and c_pro='" + c_pro + "'";
		}
		sql += " order by n03";
		List<Record> list = Db.find(sql);
		if (plist != null) {
			for (Record r : plist) {
				String pid = r.getStr(cvalue);
				// int ilayer=Integer.parseInt(r.getStr("ilayer"))+1;
				String m_id = r.getStr("m_id");
				sql = "select c01 as " + cvalue + ",c02 as " + ctext + ",m_id,n03 from eam_sblx where   m_id='" + pid
						+ "'";
				if (StrUtil.isNotEmpty(c_pro)) {
					sql += " and c_pro='" + c_pro + "'";
				}

				sql += " order by n03";
				List<Record> clist = Db.find(sql);
				if (clist.size() > 0) {
					r.set("open", true);
					r.set("children", getTree(cvalue, ctext, clist, c_pro));
				}
			}

			return plist;
		} else {
			return getTree(cvalue, ctext, list, "", c_pro);
		}
	}

	public List<Record> getFlTree(String cvalue, String ctext, List<Record> plist, String bmclass, String c_pro) {
		String sql = "select c01 as " + cvalue + ",c02 as " + ctext + ",m_id,n03 from eam_sblx where m_id='0'";
		if (StrUtil.isNotEmpty(c_pro)) {
			sql += " and c_pro='" + c_pro + "'";
		}
		sql += " order by n03";
		List<Record> list = Db.find(sql);
		if (plist != null) {
			for (Record r : plist) {
				String ccode = r.getStr(cvalue);
				// int ilayer=Integer.parseInt(r.getStr("ilayer"))+1;
				String m_id = ccode;
				sql = "select c01 as " + cvalue + ",c02 as " + ctext + ",m_id,n03 from eam_sblx where  m_id='" + m_id
						+ "' ";
				if (StrUtil.isNotEmpty(c_pro)) {
					sql += " and c_pro='" + c_pro + "'";
				}
				sql += " order by n03";
				List<Record> clist = Db.find(sql);
				if (clist.size() > 0) {
					r.set("open", true);
					r.set("children", getFlTree(cvalue, ctext, clist, c_pro));
				}
			}

			return plist;
		} else {
			return getFlTree(cvalue, ctext, list, c_pro);
		}
	}

}
